﻿using System;
using System.Collections.Generic;
using System.Text;
using Common;
using UnityEngine;

partial class SoulManager
{
    public ActionModule Module;
    public SoulManager(ActionModule module)
    {
        Module = module;
    }
    public void Update()
    {
        int deleteIndex = -1;
        for (int i = 0; i < souls.Count; i++)
        {
            souls[i].G.transform.position += Vector3.up * InputManager.DeltaTime;
            if (Mathf.Abs(souls[i].G.transform.position.y - souls[i].P.y) >= 10)
            {
                deleteIndex = i;
            }
        }
        if (deleteIndex > -1)
        {
            souls[deleteIndex].G.GetComponent<ParticleEmitter>().emit = false;
            souls.RemoveAt(deleteIndex);
        }
    }
    public void FixedUpdate()
    {
        
    }
    public void LateUpdate()
    {
        
    }

    private List<SoulInstance> souls = new List<SoulInstance>();
    #region ISoulManager Members

    public void AddSoul(Vector3 v)
    {
        SoulInstance soul = new SoulInstance();
        soul.G = GameObject.Instantiate(Resources.Load("Effect/soul"), v, Quaternion.identity) as GameObject;
        soul.P = v;
        souls.Add(soul);
    }

    public void SuckSoul(Vector3 v)
    {
        for (int i = 0; i < souls.Count; i++)
        {
            float speed = Mathf.Abs(souls[i].G.transform.position.z - v.z) + Mathf.Abs(souls[i].G.transform.position.y - v.y);
            if (speed <= .1f)
            {
                souls[i].G.GetComponent<ParticleEmitter>().emit = false;
                souls.RemoveAt(i);
                break;
            }
            speed = 1 / speed;
            souls[i].G.transform.position = Vector3.Lerp(souls[i].G.transform.position, v, speed * InputManager.DeltaTime * 10);
        }
    }

    #endregion
}
public class SoulInstance
{
    public GameObject G;
    public Vector3 P;
    //public void Delete
}

